/*年轻人，只管向前看，不要管自暴自弃者的话*/

import 'package:flutter/services.dart';

///create by itz on 2024/1/17 15:16
///desc :
class FlutterBridge {
  /// Android Flutter 通讯 方法通道名称
  static const String METHOD_CHANNEL_NAME = "FLUTTER_BRIDGE_METHOD_CHANNEL";

  /// Android Flutter 通讯 flutter to android 方法名称
  static const String METHOD_NAME_FLT_TO_AND = "METHOD_NAME_FLUTTER_TO_ANDROID";

  /// Android Flutter 通讯 android to flutter 方法名称
  static const String METHOD_NAME_AND_TO_FLT = "METHOD_NAME_AND_TO_FLT";

  /// Flutter使用AndroidView viewTypeId名称
  static const String VIEW_TYPE_ID_ANDROID_VIEW = "VIEW_TYPE_ID_ANDROID_VIEW";

  static final FlutterBridge _instance = FlutterBridge._();
  final MethodChannel _bridge = const MethodChannel(METHOD_CHANNEL_NAME);
  final _listener = {};

  FlutterBridge._() {
    _bridge.setMethodCallHandler((call) {
      String method = call.method;
      if (_listener[method] != null) {
        return _listener[method](call);
      }
      return Future.value();
    });
  }

  static FlutterBridge getInstance() {
    return _instance;
  }

  register(String method, Function(MethodCall) callback) {
    _listener[method] = callback;
  }

  unRegister(String method) {
    _listener.remove(method);
  }

  MethodChannel get bridge => _bridge;

  flutterToAndroid([Map? params]) {
    _bridge.invokeMethod(METHOD_NAME_FLT_TO_AND, params);
  }
}
